<template>
  <el-row type="flex" justify="space-between" align="middle" class="row" :class="isRoot ? 'row-boder-bottom' : ''">
    <el-col>
      <span>
        <svg-icon :icon-class="isRoot ? 'university' : treeNode.children ? (treeNode.expanded ? 'jian' : 'jia') : 'man'" />
        {{ treeNode.name }}
      </span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作
              <i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
export default {
  props: {
    treeNode: {
      required: true,
      type: Object // 必须有name属性和manager属性
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 处理下拉选项的点击行为
    handleCommand(type) {
      if (type === 'add') {
        // 添加子部门
        // 触发父组件自定义事件更改添加组件的显示和隐藏状态,同时把该操作的部门数据传递到父组件中，我们才知道是要给哪个部门添加子部门
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        // 编辑部门
        this.$emit('editDepts', this.treeNode)
      } else if (type === 'del') {
        // 删除部门
        this.$confirm('您确定要删除该部门吗', '删除部门', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 确认删除
          // 触发父组件中的自定义事件
          this.$emit('delDepart', this.treeNode.id)
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.row {
  width: 100%;
  height: 40px;
}
.row-boder-bottom {
  border-bottom: 1px solid #ccc;
}
</style>
